package com.hoshino.momochi.controller;

import com.hoshino.momochi.annotation.IgnoreLogin;
import com.hoshino.momochi.mapper.BlogTagMapper;
import com.hoshino.momochi.model.DTO.TagDTO;
import com.hoshino.momochi.model.domain.Tag;
import com.hoshino.momochi.service.BlogTagService;
import com.hoshino.momochi.service.TagService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
public class TagController {

    @Autowired
    private BlogTagService blogTagService;

    @Autowired
    private TagService tagService;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 增加新的标签
     * tag_names
     *
     * @return
     */
    @RequestMapping("api/tag/addTags")
    public JsonResponse addTags(HttpServletRequest request, @RequestBody TagDTO tagDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        if (uid != 1) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        List<String> sl = tagDTO.getTag_names();
        tagService.addListTag(sl);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取 标签列表
     *
     * @return
     */
    @RequestMapping("api/tag/getAllTags")
    @IgnoreLogin
    public JsonResponse getAllTags() {
        List<Tag> list = tagService.getAllTagList();
        return JsonResponse.success(list, CodeEnum.SUCCESS.getMessage());
    }

}
